package handler

import (
	"car-trade-mgr/cache"
	"car-trade-mgr/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carCode := c.PostForm("car_code")
	carName := c.PostForm("car_name")
	carPrice, _ := strconv.Atoi(c.PostForm("car_price"))
	if carCode == "" || carName == "" || carPrice == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	car := model.Car{
		CarCode:  carCode,
		CarName:  carName,
		CarPrice: carPrice,
	}
	err := model.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func CarOrderAdd(c *gin.Context) {
	orCode := c.PostForm("or_code")
	orPrice, _ := strconv.Atoi(c.PostForm("or_price"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if orCode == "" || orPrice == 0 || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	carOrder := model.CarOrder{
		OrCode:  orCode,
		OrPrice: orPrice,
		Cid:     cid,
	}
	err := model.DB.Create(&carOrder).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
			SELECT
		  car_orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_price as CarPrice,
		  car_orders.or_code as OrCode,
		  car_orders.or_price as OrPrice
		FROM
		  car_orders
		LEFT JOIN cars ON cars.id = car_orders.cid;
	*/
	var list []model.CarOrderList
	redis_key := "car_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_price as CarPrice",
				"car_orders.or_code as OrCode",
				"car_orders.or_price as OrPrice",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.CarOrderList
	redis_key := "car_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_price as CarPrice",
				"car_orders.or_code as OrCode",
				"car_orders.or_price as OrPrice",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.cid = ?", id).
			Find(&list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
